package com.cwj.thread;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenwujie
 * @date 2020-12-31 16:03
 */
public class BitThreadTest {
    private int a;
    private int b;
    private int c;
    private int n;

    public static void main(String[] args) throws InterruptedException {
        BitThreadTest bitThreadTest = new BitThreadTest();

        List<Thread> l = new ArrayList<>(10);
        for (int i = 0; i < 100; i++) {

            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    bitThreadTest.setC1();
                    bitThreadTest.setC2();
                    synchronized (BitThreadTest.class){
                        bitThreadTest.setA(bitThreadTest.getA() + 1);
                        bitThreadTest.setB(bitThreadTest.getB() + 1);

                        int n = bitThreadTest.getN();
                        if(n >= 200){
                            System.out.println(n);

                        }
                    }
                    int c = bitThreadTest.getC();
                    System.out.println("c: " + c);
                }
            });
            l.add(t1);
        }
        for (Thread thread : l) {
            thread.start();
        }
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return ((c & 0xffff0000) >> 16)+ (c & 0xffff);
    }

    public void setC(int c) {
        this.c = c;
    }


    public int getN() {
        return a + b ;
    }

    public void setC1(){
        c = c + (1 << 16);
    }

    public void setC2(){
        c = c + 1;
    }

}
